/*
 *   Author:
 *   vyouzhi <vyouzhi@163.com>
 *   http://www.xda.cn
 *
 *   File:                    link.c
 *   Create Date:      2011-07-03 22:37:06
 */
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>

#include "link.h"

cliInfo *link_init(void){
	cliInfo *s = (struct client_info *)calloc(1, sizeof(struct client_info));
	if(s == NULL){
		printf("link_init error can't calloc\n");
		exit(-1);
	}
	return s;
}

void link_insert(cliInfo *dist, cliInfo *src){
	cliInfo *s = dist;
	while(s!=NULL && s->next!=NULL){
		s = s->next;
	}
	s->next = src;
	/*s = src;*/
}

void link_del(cliInfo *link_head, int csfd){
	cliInfo *s, *l;
	l = s = link_head;
	while(s != NULL){
		if (s->clientSockfd == csfd){
			l->next = s->next;
			/*free(s->recvbuff);*/
			bzero(s->data, strlen(s->data));
			bzero(s->recvbuff, strlen(s->recvbuff));
			free(s);
			break;
		}
		if(s->next == NULL)break;
		l = s;		
		s = s->next;
	}
}

void link_free(cliInfo *link_head){
	cliInfo *s, *l;
	s = link_head;
	while(s!=NULL){
		l = s->next;
		/*free(s->data);*/
		free(s);
		s = NULL;
		s = l;
	}
}

cliInfo *link_find(cliInfo *link_head, int csfd){
	cliInfo *s;
	s = link_head;
	while(s!=NULL){
		if(s->clientSockfd == csfd){
			break;
		}
		s = s->next;
	}
	return s;
}

int link_len(cliInfo *link_head){
	cliInfo *s;
	int len=0;
	s = link_head;
	while(s!=NULL) {
		++len;
		s = s->next;
	}
	return len;
}
/*
int main(int argc, char* argv[]){
   	cliInfo *h, *t, *p, *c;
	int i = 0, m=0;
	h = link_init();
	for(i=0; i<10; i++){
		t = link_init();
		t->clientSockfd = i+1;
		t->recvCount = 1;
		t->recvStatus = 1;
		t->data = (char *)calloc(10, sizeof(char));
		if(t->data != NULL)
			strcpy(t->data, "data");
		t->next = NULL;
		//if(i==0)h=t;
		//else
		link_insert(h, t);
	}
	p = h;
	m=link_len(p);
	for(i=0; i<m; i++){
		printf("%d: csfd is %s\n",i, p->data);
		p = p->next;
	}
	link_del(h, 3);

	printf("del ---------------\n");
	
	p = h;
	printf("now link len %d\n", link_len(p));
	m = link_len(p);
	for(i=0; i<m; i++){
		printf("%d: csfd is %d\n",i, p->clientSockfd);
		p = p->next;
	}
	printf("chang--------------\n");
	c = link_find(h, 5);
	c->clientSockfd = 20;

	p = h;
	m = link_len(p);
	for(i=0; i<m; i++){
		printf("%d: csfd is %d\n",i, p->clientSockfd);
		p = p->next;
	}
	link_free(h);
	printf("now link len is %d\n", link_len(p));
	return (0);
}*/

/* vim: set ts=4 sw=4: */

